package com.hamster.crmpre.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hamster.crmpre.pojo.DepartmentAndPosition;
import com.hamster.crmpre.pojo.PhoneUser;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * @author Tarry
 * @create 2019/12/25 10:51
 */
@Repository
@Mapper
public interface PhoneUserMapper extends BaseMapper<PhoneUser> {
    //根据openid查询用户是否存在
    @Select("select * from phone_user where openid = #{id}")
    PhoneUser getUserByOpenId(String id);

    /**
     * 查询员工表
     * @param phone
     * @return
     */
    @Select("select * from employee where phone = #{phone}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "department",column = "department"),
            @Result(property = "position",column = "position",one = @One(select="com.hamster.crmpre.mapper.PhoneUserMapper.queryEPositionById")),
            @Result(property = "name",column = "name")
    })
    DepartmentAndPosition queryEmployee(@Param("phone") String phone);


    /**
     * 根据编号查询当前员工的职位
     * @param id
     * @return
     */
    @Select("select title from dictionary_position where id = #{id}")
    String queryPositionById(String id);

    /**
     * 根据编号查询当前员工的职位【英文名称】
     * @param id
     * @return
     */
    @Select("select e_title from dictionary_position where id = #{id}")
    String queryEPositionById(String id);



    /**
     * 查询部门名称,根据部门编号
     */
    @Select("select title from dictionary_department where id = #{id}")
    String queryNameByDepartment(String id);

    /**
     * 查询客户表【所选项目id数组】
     */
    @Select("select project_id from customer where phone = #{phone}")
    String queryCustomer(@Param("phone")String phone);




    /**
     * 查询项目的 分类名称
     * @param id
     * @return
     */
    @Select("select B.title from project A,dictionary_project B where A.id =#{id} and A.category_id = B.id")
    String queryProjectCategoryName(String id);

    @Update("update phone_user set phone = #{phone} where openid = #{openid}")
    void setPhone(@Param("openid") String openId,@Param("phone") String phone);

    /**
     * 查询姓名
     * @param phone
     * @return
     */
    @Select("select name from customer where phone = #{phone}")
    String queryCustomerName(String phone);

    @Select("select count(*) from phone_user where phone = #{phone}")
    Integer checkPhone(@Param("phone") String phone);

}
